package com.example.demo.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.component.StringHelper;
import com.example.demo.component.*;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class BucnUtil {

    private static String defautLicenseId = "1";

    public static void setDefaultLicenseId(String id) {
        defautLicenseId = id;
    }

    public static <T> ListResult<T> page2ListResult(IPage<T> page, List<T> list) {
        ListResult<T> result = new ListResult<>();
        result.setRows(list);
        result.setRecords((int) page.getTotal());
        result.setPages((int) page.getPages());
        result.setPageindex((int) page.getCurrent());
        result.setResult("1");
        result.setMsg("查询成功");
        return result;
    }

    public static void throwMessage(String msgId, Object... placeHolders) {
        String message = "抛出异常信息";
        throw BucnException.create(message);
    }



    public static void userWrapInsert(Object param, String token) {
        UserEntity userEntity = tokenUser(token);
        if (param == null) {
            return;
        }
        if (param instanceof Map) {
            Map map = (Map) param;
            map.put("createdName", userEntity.getUserName());
            map.put("modifyName", userEntity.getUserName());
            map.put("modifier", userEntity.getUserID());
            map.put("creator", userEntity.getUserID());
            map.put("updateControlId", StringHelper.GetGUID());
            map.put("createdDate", LocalDateTime.now());
            map.put("lastUpdatedDate", LocalDateTime.now());
            String licenseIdString = (userEntity.getTenancyList() != null && userEntity.getTenancyList().size() > 0)
                    ? defautLicenseId
                    : defautLicenseId;
            map.put("licenseId", licenseIdString);
        } else {
            Class<?> clazz = param.getClass();
            try {
                clazz.getMethod("setCreatedName", String.class).invoke(param, userEntity.getUserName());
                clazz.getMethod("setModifyName", String.class).invoke(param, userEntity.getUserName());
                clazz.getMethod("setModifier", String.class).invoke(param, userEntity.getUserID());
                clazz.getMethod("setCreator", String.class).invoke(param, userEntity.getUserID());
                clazz.getMethod("setUpdateControlId", String.class).invoke(param, StringHelper.GetGUID());
                clazz.getMethod("setCreatedDate", String.class).invoke(param, LocalDateTime.now());
                clazz.getMethod("setLastUpdatedDate", String.class).invoke(param, LocalDateTime.now());
            } catch (Exception e) {

            }
        }
    }

    public static void userWrapUpdate(Object param, String token) {
        UserEntity userEntity = tokenUser(token);
        if (param == null) {
            return;
        }
        if (param instanceof Map) {
            Map map = (Map) param;
            map.put("modifyName", userEntity.getUserName());
            map.put("modifier", userEntity.getUserID());
            map.put("lastUpdatedDate", LocalDateTime.now());
        } else {
            Class<?> clazz = param.getClass();
            try {
                clazz.getMethod("setModifyName", String.class).invoke(param, userEntity.getUserName());
                clazz.getMethod("setModifier", String.class).invoke(param, userEntity.getUserID());
                clazz.getMethod("setLastUpdatedDate", String.class).invoke(param, LocalDateTime.now());
            } catch (Exception e) {

            }
        }
    }

    public static UserEntity tokenUser(String token) {
        UserEntity userEntity = (UserEntity) SessionHelper.get(token);
        if (userEntity == null) {
            userEntity = new UserEntity();
            userEntity.setEmpID("1");
            userEntity.setEmpName("1");
        }
        return userEntity;
    }

}
